package com.qf.service.impl;

import com.qf.dao.DiariesDao;
import com.qf.entity.Diaries;
import com.qf.service.DiariesService;
import com.qf.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 日记(Diaries)表服务实现类
 */
@Service("diariesService")
public class DiariesServiceImpl implements DiariesService {

    @Autowired
    private DiariesDao diariesDao;

    //增
    @Override
    public R addDiaries(Diaries diaries) {
        if (diaries.getContent() == null || diaries.getContent().isEmpty()) {
            return R.fail("日记内容不能为空");
        }

        diaries.setCreatedTime(new Date());
        // 插入日记数据
        int result = diariesDao.insertDiary(diaries);
        if (result > 0) {
            return R.ok("日记新增成功");
        } else {
            return R.fail("日记新增失败");
        }
    }

    //删
    @Override
    public R deleteDiaries(Diaries diaries) {
        int result = diariesDao.deleteDiaryById(diaries);
        if (result > 0) {
            return R.ok("日记删除成功");
        } else {
            return R.fail("日记删除失败");
        }
    }

    //改
    @Override
    public R updateDiaries(Diaries diaries) {
        try {
            int result = diariesDao.updateDiaries(diaries);
            if (result > 0) {
                return R.ok("日记修改成功");
            } else {
                return R.fail("日记修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("日记修改过程中出现异常：" + e.getMessage());
        }
    }


    @Override
    public R<List<Diaries>> selectDiaries(Integer userId) {

        List<Diaries> diaryList = diariesDao.selectByDiaries(userId);
        if (diaryList != null && !diaryList.isEmpty()) {
            return R.ok(diaryList);
        } else {
            return R.fail(null);
        }
    }


}


/**
 * 新增日记
 *
 * @param diaries
 * @return 根据日记id来删除日记
 * @param id
 * @return 根据日记id来修改日记内容
 * @param id
 * @return 根据日记的每个属性来动态查询日记
 * @param diaries
 * @return 根据日记id来删除日记
 * @param id
 * @return 根据日记id来修改日记内容
 * @param id
 * @return 根据日记的每个属性来动态查询日记
 * @param diaries
 * @return
 */
   /* public R add (Diaries diaries){


        return null;
    }

    *//**
 * 根据日记id来删除日记
 * @param id
 * @return
 *//*
    public R deleteById(Integer id){


        return null;
    }

    *//**
 * 根据日记id来修改日记内容
 * @param id
 * @return
 *//*
   public R updateById(Integer id){


        return null;

    }

    *//**
 * 根据日记的每个属性来动态查询日记
 * @param diaries
 * @return
 *//*
    public R selectByDiaries(Diaries diaries){

       return null;
    }*/



